/**************************************************************************/
/*                                                                        */
/*          	Portions Copyright (C)2013, Jason Whelan                  */
/*                                                                        */
/**************************************************************************/
/*                                                                        */
/*                              WWIV Version 5.0x                         */
/*             Copyright (C)1998-2007, WWIV Software Services             */
/*                                                                        */
/*    Licensed  under the  Apache License, Version  2.0 (the "License");  */
/*    you may not use this  file  except in compliance with the License.  */
/*    You may obtain a copy of the License at                             */
/*                                                                        */
/*                http://www.apache.org/licenses/LICENSE-2.0              */
/*                                                                        */
/*    Unless  required  by  applicable  law  or agreed to  in  writing,   */
/*    software  distributed  under  the  License  is  distributed on an   */
/*    "AS IS"  BASIS, WITHOUT  WARRANTIES  OR  CONDITIONS OF ANY  KIND,   */
/*    either  express  or implied.  See  the  License for  the specific   */
/*    language governing permissions and limitations under the License.   */
/*                                                                        */
/**************************************************************************/

#include "wwiv.h"
#include "TextUI.h"
#include "sys_general.h"

class SysPWCommand : public UICommand
{
    virtual bool Execute()
    {
        UIDesktop *desktop = UIDesktop::GetDesktop();
		UIInputBox inputBox(desktop, 5, 40, 5, 20, "Enter NEW System Password:", "Input Password", '*', sizeof(syscfg.systempw)-1);
		
		inputBox.Run();
		
		std::string result = inputBox.GetText();
		
		strcpy(syscfg.systempw, result.c_str());
		
		desktop->GetStatusBar()->Write(" changed to - " +result);
        return true;
    }
};

class SysOpNameCommand : public UICommand
{
    virtual bool Execute()
    {
        UIDesktop *desktop = UIDesktop::GetDesktop();
		UIInputBox inputBox(desktop, 5, 40, 5, 20, "Enter SysOp Name:", "Input Name", NULL, sizeof(syscfg.sysopname)-1);
		
		inputBox.Run();
		
		std::string result = inputBox.GetText();
		
		strcpy(syscfg.sysopname, result.c_str());
		
		desktop->GetStatusBar()->Write(" > " +result);
        return true;
    }
};

class SysOpHighTimeCommand : public UICommand
{
    virtual bool Execute()
    {
        UIDesktop *desktop = UIDesktop::GetDesktop();
		UIInputBox inputBox(desktop, 5, 40, 5, 20, "Enter SysOp High Time:", "Input Time");
		
		inputBox.Run();
		
		std::string result = inputBox.GetText();
		
		//strcpy(syscfg.sysophightime, result.c_str());
		
		desktop->GetStatusBar()->Write(" > " +result);
        return true;
    }
};

class SysOpLowTimeCommand : public UICommand
{
    virtual bool Execute()
    {
        UIDesktop *desktop = UIDesktop::GetDesktop();
		UIInputBox inputBox(desktop, 5, 40, 5, 20, "Enter SysOp Low Time:", "Input Time");
		
		inputBox.Run();
		
		std::string result = inputBox.GetText();
		
		//strcpy(syscfg.sysoplowtime, result.c_str());
		
		desktop->GetStatusBar()->Write(" > " +result);
        return true;
    }
};

class SystemNameCommand : public UICommand
{
    virtual bool Execute()
    {
        UIDesktop *desktop = UIDesktop::GetDesktop();
		UIInputBox inputBox(desktop, 5, 40, 5, 20, "Enter BBS Name:", "Input Name", NULL, sizeof(syscfg.systemname)-1);
		
		inputBox.Run();
		
		std::string result = inputBox.GetText();
		
		strcpy(syscfg.systemname, result.c_str());
		
		desktop->GetStatusBar()->Write(" > " +result);
        return true;
    }
};

class SysPhoneCommand : public UICommand
{
    virtual bool Execute()
    {
        UIDesktop *desktop = UIDesktop::GetDesktop();
		UIInputBox inputBox(desktop, 5, 40, 5, 20, "Enter System Phone Number:", "Input Number", NULL, sizeof(syscfg.systemphone)-1);
		
		inputBox.Run();
		
		std::string result = inputBox.GetText();
		
		strcpy(syscfg.systemphone, result.c_str());
		
		desktop->GetStatusBar()->Write(" > " +result);
        return true;
    }
};

class ClosedSystemCommand : public UICommand
{
    virtual bool Execute()
    {
        UIDesktop *desktop = UIDesktop::GetDesktop();
		UIInputBox inputBox(desktop, 5, 40, 5, 20, "Enter Closed System:", "Input Status");
		
		inputBox.Run();
		
		std::string result = inputBox.GetText();
		
		//strcpy(syscfg.closedsystem, result.c_str());
		
		desktop->GetStatusBar()->Write(" > " +result);
        return true;
    }
};

class NewUserPWCommand : public UICommand
{
    virtual bool Execute()
    {
        UIDesktop *desktop = UIDesktop::GetDesktop();
		UIInputBox inputBox(desktop, 5, 40, 5, 20, "Enter New User Password:", "Input Password" , '*', sizeof(syscfg.newuserpw)-1);
		
		inputBox.Run();
		
		std::string result = inputBox.GetText();
		
		strcpy(syscfg.newuserpw, result.c_str());
		
		desktop->GetStatusBar()->Write(" > " +result);
        return true;
    }
};

bool SystemGeneral::Execute()
{
    UIDesktop *desktop = UIDesktop::GetDesktop();
    UIMenu *menu = new UIMenu( "General Information" );
	menu->Add(new UIMenuItem( "System Password", new SysPWCommand ) );
	menu->Add(new UIMenuItem( "Sysop Name", new SysOpNameCommand ) );
	menu->Add(new UIMenuItem( "Sysop high time", new SysOpHighTimeCommand ) );
	menu->Add(new UIMenuItem( "Sysop low time", new SysOpLowTimeCommand ) );
	menu->Add(new UIMenuItem( "System Name", new SystemNameCommand ) );
	menu->Add(new UIMenuItem( "System Phone", new SysPhoneCommand ) );
	menu->Add(new UIMenuItem( "Closed System", new ClosedSystemCommand ) );
	menu->Add(new UIMenuItem( "Newuser PW", new NewUserPWCommand ) );
	menu->Add(new UIMenuItem( "Newuser restrict", NULL ) );
	menu->Add(new UIMenuItem( "Newuser SL", NULL ) );
	menu->Add(new UIMenuItem( "Newuser SSL", NULL ) );
	menu->Add(new UIMenuItem( "Network high time", NULL ) );
	menu->Add(new UIMenuItem( "Network low time", NULL ) );
	menu->Add(new UIMenuItem( "Up/Download ratio", NULL ) );
	menu->Add(new UIMenuItem( "Post/Call ratio", NULL ) );
	menu->Add(new UIMenuItem( "Max waiting", NULL ) );
	menu->Add(new UIMenuItem( "Max users", NULL ) );
	menu->Add(new UIMenuItem( "Caller number", NULL ) );
	menu->Add(new UIMenuItem( "Days active", NULL ) );
	UIView *currentActiveView = desktop->GetActiveView();
    UIPopupMenu *popup = menu->ShowPopupMenu( desktop, 27, 3 );

    while( true )
    {
        int key = GetKey();
		if(key == 0x1b) break;
		popup->ProcessKeyEvent(key);
    }

    menu->HidePopupMenu();

    delete menu;

	desktop->SetActiveView(currentActiveView);

    return false;
}

int SystemGeneral::GetKey()
{
    while ( true )
    {
        int key = getch();
        if ( key != ERR )
        {
            return key;
        }
    }
    return ERR;
}

